package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result list(ClazzQueryParam  param) {
        log.info("查询班级列表");
        //开启分页查询
        Page<Clazz> page = new Page(param.getPage(), param.getPageSize());
        //编写分页查询的排序
        page.addOrder(OrderItem.desc("update_time"));
        //添加查询条件
        LambdaQueryWrapper<Clazz> cqw = new LambdaQueryWrapper<>();
        cqw.like(param.getName() != null && param.getName().length() > 0,Clazz::getName,param.getName());
        cqw.between(param.getBegin() != null && param.getEnd() != null,Clazz::getBeginDate,param.getBegin(),param.getEnd());
        //调用service层分页查询
        page = clazzService.page(page,cqw);
        //还需要把班主任的名字查出来
        List <Clazz> clazzes = page.getRecords();

        Set <Integer> masterId = clazzes.stream().map(Clazz::getMasterId).filter(id -> id != null).collect(Collectors.toSet());
        if(masterId!=null && masterId.size()>0){
            //查询收集起来来的masterid对应的员工
            List<Emp> emps = empService.listByIds(masterId);
            Map<Integer, String> map = emps.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));
            clazzes.forEach(clazz->{
                clazz.setMasterName(map.get(clazz.getMasterId()));
            });
        }
        PageResult<Clazz> result = new PageResult<>(page.getTotal(), clazzes);

        return Result.success(result);
    }

    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("新增班级：{}", clazz);
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());

        clazzService.save(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级：{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级：{}", clazz);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级：{}", id);
        clazzService.removeById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询全部班级");
        List<Clazz> list = clazzService.list();
        return Result.success(list);
    }
}
